package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.constant.state.ManagerStatus;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.system.entity.SysAgent;
import cn.stylefeng.guns.modular.system.entity.SysSchool;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.factory.UserFactory;
import cn.stylefeng.guns.modular.system.mapper.SysAgentMapper;
import cn.stylefeng.guns.modular.system.model.AgentDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 代理人用户管理表 服务类
 * </p>
 *
 * @author zfy
 * @since 2020-08-08
 */
@Service
public class SysAgentService extends ServiceImpl<SysAgentMapper,SysAgent> {
    @Autowired
    private UserService userService;

    public IPage<AgentDto> selectUsers(String name, Integer examine, Long userId) {
        if(ToolUtil.isEmpty(userId)){
            userId = ShiroKit.isAdmin() ? null : ShiroKit.getUserNotNull().getId();
        }
        return this.baseMapper.agentList(LayuiPageFactory.defaultPage(),name,examine,userId);
    }
    public SysAgent getSysAgent (Long userId){
        SysAgent sysAgent = this.getOne(new QueryWrapper<SysAgent>()
                .eq("user_id", userId));
        return ToolUtil.isEmpty(sysAgent)?new SysAgent():sysAgent;
    }

    public void agentSave(AgentDto agentDto) {
        // 判断账号是否重复
        User theUser = userService.getByAccount(agentDto.getAccount());
        if (theUser != null
                && (ToolUtil.isEmpty(agentDto.getUserId()) || !theUser.getUserId().equals(agentDto.getUserId()))) {
            throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG);
        }
        // 完善账号信息
        String salt = "";
        String password = "";
        if(ToolUtil.isNotEmpty(agentDto.getPassword())){
            salt = ShiroKit.getRandomSalt(5);
            password = ShiroKit.md5(agentDto.getPassword(), salt);
        }
        agentDto.setStatus(ManagerStatus.OK.getCode());
        User user = UserFactory.createUser(agentDto,password,salt);
        user.setPhone(user.getAccount());
        user.setRoleId("1291986078187773954");
        user.setExamine(0);
        userService.saveOrUpdate(user);

        SysAgent agent = getSysAgent(user.getUserId());
        BeanUtils.copyProperties(agentDto, agent);
        agent.setUserId(user.getUserId());
        agent.setCreateUser(1L);
        agent.setCreateTime(LocalDateTime.now());
        this.saveOrUpdate(agent);
    }
}
